from flask_login import UserMixin
from sqlalchemy import Column, DateTime, Enum, ForeignKey, Integer, String, func, text
from sqlalchemy.dialects.mysql import (
    BIGINT,
    DATE,
    MEDIUMTEXT,
    TIMESTAMP,
    TINYINT,
    VARCHAR,
)
from sqlalchemy.orm import relationship
from werkzeug.security import check_password_hash, generate_password_hash

from app.database.base import Base


class User(Base):

    __tablename__ = "user"

    id = Column(Integer, primary_key=True)
    telegram_id = Column(BIGINT)
    is_active = Column(TINYINT(1), nullable=False, server_default="1")
    accounts = relationship("Account", back_populates="user")
    tasks = relationship("Task", back_populates="user")
    documents = relationship("Document", back_populates="user")
    unique = relationship("Unique", back_populates="user")
    whitepages = relationship("Whitepage", back_populates="user")
    id_accounts = relationship("IdAccount", back_populates="user")
    balance = Column(Integer, server_default="0")
    accounts_added = Column(Integer, server_default="0")
    likes = Column(Integer, server_default="0")
    comments = Column(Integer, server_default="0")
    uniques = Column(Integer, server_default="0")
    documents_generated = Column(Integer, server_default="0")
    whitepages_generated = Column(Integer, server_default="0")
    wlo_accs_reged = Column(Integer, server_default="0")
    dolphin_accs_loaded = Column(Integer, server_default="0")
    linked_shop = Column(TINYINT(1), nullable=False, server_default="0")
    capmonster_token = Column(String(100))
    kopeechka_token = Column(String(100))
    proxies = Column(VARCHAR(1000))
    was_registered = Column(DateTime, server_default=func.now())
    last_activity = Column(DateTime, server_default=func.now(), onupdate=func.now())

    def __repr__(self):
        return f"{self.telegram_id}"


class AdminUser(UserMixin, Base):
    __tablename__ = "admin_user"

    id = Column(Integer, primary_key=True)
    email = Column(String(100), unique=True)
    password = Column(String(100))
    name = Column(String(100))
    is_super_admin = Column(TINYINT(1), nullable=False, server_default="0")

    # Flask - Login
    @property
    def is_authenticated(self):
        return True

    @property
    def is_active(self):
        return True

    @property
    def is_anonymous(self):
        return False

    def set_password(self, password):
        self.password = generate_password_hash(password, method="sha256")

    def check_password(self, password):
        return check_password_hash(self.password, password)


class Task(Base):
    __tablename__ = "tasks"

    id = Column(Integer, primary_key=True)
    type = Column(String(length=50))
    user = relationship("User", back_populates="tasks")
    user_id = Column(Integer, ForeignKey("user.id"))
    status = Column(Enum("busy", "done"), server_default=text("'busy'"))
    link = Column(String(length=500))
    count_done = Column(Integer, server_default="0")
    count_work = Column(Integer, server_default="0")
    text = Column(MEDIUMTEXT())
    time_interval = Column(Integer, server_default="0")
    last_used = Column(TIMESTAMP(), server_default=func.now(), nullable=False)

    def __repr__(self):
        return f"Task ID:{self.id} {self.type}:{self.status}"


class Account(Base):
    __tablename__ = "db_profile_fb_dolphin"

    id = Column(Integer, primary_key=True)
    area = Column(String(50))
    user = relationship("User", back_populates="accounts")
    user_id = Column(Integer, ForeignKey("user.id"))
    Name = Column(VARCHAR(50))
    login = Column(VARCHAR(50), nullable=False)
    password = Column(VARCHAR(50), nullable=False)
    proxy = Column(MEDIUMTEXT)
    useragent = Column(MEDIUMTEXT)
    status = Column(
        Enum(
            "sliv",
            "call_friends",
            "ADS",
            "update",
            "invite",
            "checkpoint",
            "zagruzil selfy",
            "disable",
            "zagruzil doki",
            "message",
            "telephone",
            "tapto",
            "recaptcha",
            "bad_mail",
            "bad_phone",
            "email_update",
            "bad_pass",
            "changePass",
            "selfy",
            "selfy_not_load",
            "worm",
            "friends",
            "wait_aproove",
            "bad_login",
            "err_send_mes_mail",
            "zrd",
            "zrd_plus",
            "zrd_plus_up",
            "zrd_bm",
            "ads_block",
            "zrd_plus_bm",
            "bm",
            "zrd_plus_bm_not_aproove",
            "worm_bm_not_aproove",
            "worm_bm_aproove",
            "back_from_store",
        ),
        server_default=text("'worm'"),
    )
    status_execute = Column(Enum("busy", "done"), server_default=text("'busy'"))
    token = Column(MEDIUMTEXT)
    Cookies_multilogin = Column(MEDIUMTEXT)
    link_img = Column(MEDIUMTEXT)
    phone = Column(VARCHAR(50))
    instagram = Column(MEDIUMTEXT)
    city = Column(MEDIUMTEXT)
    fanpage = Column(MEDIUMTEXT)
    Friends = Column(Integer, nullable=False, server_default=text("'0'"))
    ID_akk = Column(VARCHAR(50))
    email = Column(VARCHAR(50))
    passMail = Column(VARCHAR(50))
    kopeechka_id = Column(VARCHAR(50))
    linksSelfy = Column(MEDIUMTEXT)
    col_selfy = Column(Integer, nullable=False, server_default=text("'0'"))
    col_doki = Column(Integer, nullable=False, server_default=text("'0'"))
    col_post = Column(Integer, nullable=False, server_default=text("'0'"))
    col_phone = Column(Integer, nullable=False, server_default=text("'0'"))
    workchecpoint = Column(Integer, nullable=False, server_default=text("'0'"))
    token_bm = Column(MEDIUMTEXT)
    id_bm = Column(MEDIUMTEXT)
    link_bm = Column(String(255), nullable=False, server_default=text("''"))
    doc_link = Column(MEDIUMTEXT)
    col_change_pass = Column(Integer, nullable=False, server_default=text("'0'"))
    col_get_tel = Column(Integer, nullable=False, server_default=text("'0'"))
    col_checkpoint = Column(Integer, nullable=False, server_default=text("'0'"))
    count_bm = Column(Integer, nullable=False, server_default=text("'0'"))
    ads_block = Column(TINYINT(1), nullable=False, server_default=text("'0'"))
    created_at = Column(TIMESTAMP, server_default=func.now())
    updated_at = Column(TIMESTAMP, server_default=func.now())
    activity_status = Column(TINYINT(1), nullable=False, server_default=text("'1'"))
    result_mes = Column(MEDIUMTEXT)

    def __repr__(self):
        return f"Account ID:{self.id} {self.status_execute} {self.login}:{self.password}:{self.email}"


class Document(Base):
    __tablename__ = "documents"

    id = Column(Integer, primary_key=True)
    user = relationship("User", back_populates="documents")
    user_id = Column(Integer, ForeignKey("user.id"))
    fullname = Column(String(50))
    city = Column(String(50))
    status = Column(Enum("busy", "done"), server_default=text("'busy'"))
    img = Column(String(50))

    def __repr__(self):
        return f"Document ID: {self.id} {self.fullname}:{self.status}"


class Unique(Base):
    __tablename__ = "uniques"

    id = Column(Integer, primary_key=True)
    user = relationship("User", back_populates="unique")
    user_id = Column(Integer, ForeignKey("user.id"))
    status = Column(Enum("busy", "done"), server_default=text("'busy'"))
    file_path = Column(String(255))

    def __repr__(self):
        return f"Creative ID: {self.id} {self.status}"


class Whitepage(Base):
    __tablename__ = "whitepages"

    id = Column(Integer, primary_key=True)
    user = relationship("User", back_populates="whitepages")
    user_id = Column(Integer, ForeignKey("user.id"))
    status = Column(Enum("busy", "done"), server_default=text("'busy'"))
    white_link = Column(String(255))
    white_login = Column(String(50))
    white_pass = Column(String(50))

    def __repr__(self):
        return f"Whitepage ID: {self.id} {self.status}"


class IdAccount(Base):
    __tablename__ = "id_accounts"

    id = Column(Integer, primary_key=True)
    user = relationship("User", back_populates="id_accounts")
    user_id = Column(Integer, ForeignKey("user.id"))
    status = Column(Enum("busy", "done"), server_default=text("'busy'"))
    ID_akk = Column(VARCHAR(50))
    result_mes = Column(MEDIUMTEXT)

    def __repr__(self):
        return f"Account check ID:{self.id} {self.status}"


class Statistics(Base):
    __tablename__ = "stats"

    id = Column(Integer, primary_key=True)
    days = Column(DATE, unique=True)
    accounts = Column(Integer, server_default="0")
    likes = Column(Integer, server_default="0")
    comments = Column(Integer, server_default="0")
    documents = Column(Integer, server_default="0")
    uniques = Column(Integer, server_default="0")
    whitepages = Column(Integer, server_default="0")
    dolphin_accs = Column(Integer, server_default="0")
    wlo_accs = Column(Integer, server_default="0")
    messages_distribution = Column(Integer, server_default="0")

    def __repr__(self):
        return f"Stats {self.days}"


class Message(Base):
    __tablename__ = "messages"

    id = Column(Integer, primary_key=True)
    text = Column(String(700), unique=True)
    answer = Column(String(1000))

    def __repr__(self) -> str:
        return f"({self.id}, {self.text}, {self.answer})"
